<?php
class Bars extends Zend_Controller_Plugin_Abstract
{
private $_sidebar=0;
    public function preDispatch()
		{
            if($this->_sidebar == 0){
            $this->view = new Zend_View();
            $this->view->setScriptPath( APPLICATION_PATH . "/modules/default/views/scripts");

           // $this->view->render('headerbox.phtml');
            $this->view->render('sidebar.phtml');
            $this->view->render('subscribeform.phtml');

            $this->view->placeholder('searchform')->append("<div id='search'>".new Myblog_Form_Search.'</div>');

            $q=Doctrine_Query::create()
                ->from('Myblog_Model_Articles a')
                ->select('a.title,a.article_id,a.page')
                ->where('a.visibility=?','yes')
                //->andWhereNotIn('a.page','main')
                ->orderBy('a.article_id DESC')
                //->limit('5')
                ->fetchArray();
            $count = count($q);
            $this->view->placeholder('sidebar')->append('<div class="articleslist">');
            $this->view->placeholder('sidebar')->append('<h5 class="sidebar">Последние новости</h5>');
            $this->view->placeholder('sidebar')->append('<ul>');

            $newsCount=0;
            for($i=0;$i<=$count;$i++){
                if(isset($q[$i]['page']) && $q[$i]['page'] == 'main'){
                    $this->view->placeholder('sidebar')->append('<li><a href='.$this->view->url(array('id'=>$q[$i]['article_id']),'catalog-display').'>'.$q[$i]['title'].'</a><br/></li>');
                    $newsCount++;
                }
                if($newsCount === 5){
                    break;
                }

            }

            $this->view->placeholder('sidebar')->append('</ul>');
            $this->view->placeholder('sidebar')->append('</div>');

            $this->view->placeholder('sidebar')->append('<div class="articleslist">');
            $this->view->placeholder('sidebar')->append('<h5 class="sidebar">Последние статьи</h5>');
            $this->view->placeholder('sidebar')->append('<ul>');
            $articlesCount=0;
            for($i=0;$count;$i++){
                if(isset($q[$i]['page']) && $q[$i]['page'] !== 'main'){
                    $this->view->placeholder('sidebar')->append('<li><a href='.$this->view->url(array('id'=>$q[$i]['article_id']),'catalog-display').'>'.$q[$i]['title'].'</a><br/></li>');
                    $articlesCount++;
                }
                if($articlesCount === 5){
                    break;
                }

            }
            $this->view->placeholder('sidebar')->append('</ul>');
            $this->view->placeholder('sidebar')->append('</div>');
            $auth = Zend_Auth::getInstance();
            if($auth->hasIdentity())
            {

            }else{
                $auth = Zend_Auth::getInstance();
                $data = $auth->getStorage()->read();
                $storage_data = new stdClass();
                $storage_data->status = 'guest';
                $auth->getStorage()->write($storage_data);
            }
            $status=$auth->getStorage()->read();
            if($status->status == 'guest')
            {
                $this->view->placeholder('sidebar')->append('<div id="userslogin">');
                $this->view->placeholder('sidebar')->append('<h5 class="sidebar">Авторизация</h5>');
                $this->loginform = new Myblog_Form_UserLogin;
                $this->view->placeholder('sidebar')->append($this->loginform);
                $this->view->placeholder('sidebar')->append('</div>');
            }
            if($status->status == 'user'){
                $this->view->placeholder('sidebar')->append('<div id="userbar">');
                $this->view->placeholder('sidebar')->append('<h5 class="sidebar">Профайл</h5>');
                $this->view->placeholder('sidebar')->append('<h4>'.$status->login.'</h4>');
                $this->view->placeholder('sidebar')->append('</div>');
            }
            if($status->status == 'admin' || $status->status == 'demo_admin'){

                $this->view->placeholder('sidebar')->append('<div id="userbar">');
                $this->view->placeholder('sidebar')->append('<h5 class="sidebar">Профайл</h5>');
                $this->view->placeholder('sidebar')->append('<h4>'.$status->status.'</h4>');
                $this->view->placeholder('sidebar')->append('</div>');
            }
                $this->view->placeholder('sidebar')->append("<div id='subscribe'>");
                $this->view->placeholder('sidebar')->append('<h6 class="sidebar">Получать новые новости и статьи на E-mail</h6>');
                $this->view->placeholder('sidebar')->append(new Myblog_Form_Subscribe);
                $this->view->placeholder('sidebar')->append('</div>');
            $this->_sidebar=1;
           }
		}
}